package com.wzg.security.one;

import java.io.UnsupportedEncodingException;

/**
 * @author henryGui
 * @version V1.0
 * @Title: TestOne
 * @Package com.wzg.security.one
 * @Description: TODO
 * @date 2018-07-09 下午4:42
 */
public class TestOne {
    public static void main(String[] args) throws Exception {
        String environment = "uat";

        String data = "这是一条测试字符串！";

        String keyStorePath = "D:/Citificate/testKey/test.keystore";
        String alias = "test";
        String password = "cas123";
        String certificatePath = "D:/Citificate/testKey/test.cer";

        certificatePath = "/Users/wuzhenggui/work/IdeaProjects/mayun/spring-base-parent/java-key-security-demo/src/main/resources/test.crt";
        keyStorePath = "/Users/wuzhenggui/work/IdeaProjects/mayun/spring-base-parent/java-key-security-demo/src/main/resources/test.keystore";


        // 私钥签名——公钥验证签名

        System.err.println("私钥签名——公钥验证签名");

        // 产生签名
        String sign = CertificateUtils.signToBase64(data.getBytes("utf-8"), keyStorePath, alias, password);
        System.out.println("私钥签名:" + sign);

        boolean status = CertificateUtils.verifySign(data.getBytes("utf-8"), sign, certificatePath);
        System.err.println("公钥验签结果:" + status);

        // 公钥加密——私钥解密
        System.out.println("\n公钥加密——私钥解密");
        byte[] encrypt = CertificateUtils.encryptByPublicKey(data.getBytes("utf-8"), certificatePath);
        String encode = Base64Util.encode(encrypt);
        System.out.println("公钥加密：" + encode);
        // 下面是安卓加密后的结果，使用 Cipher.getInstance("RSA/None/PKCS1Padding")
        byte[] decrypt = CertificateUtils.decryptByPrivateKey(Base64Util.decode(encode), keyStorePath, alias, password);
        String outputStr = new String(decrypt);

        System.out.println("解密后:" + outputStr);
    }
}
